import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;

import com.thoughtworks.xstream.annotations.XStreamAlias;

public class Collection {

	@XStreamAlias("list-of-films")
	private ArrayList<Movie> films;
	@XStreamAlias("number-of-films")
	private int numFilms;
	private double storageTotal;
	@XStreamAlias("file-path")
	private String[] filePath;
	private boolean filePathSet;
	private String exportPath;
	
	public Collection()
	{
		films = new ArrayList<Movie>();
		numFilms = 0;
		storageTotal = 0;
		filePathSet = false;
	}
	
	public void setFilePath(String[] fp)
	{
		filePath = fp;
		filePathSet = true;
		
	}
	
	public boolean isFilePathSet()
	{
		return filePathSet;
	}
	
	public void setFilePathSet(boolean i)
	{
		filePathSet = i;
	}
	
	public int getSize()
	{
		return films.size();
	}
	public String getElementAt(int index)
	{
		return films.get(index).getFileName();
	}
	public String getLocationAt(int index)
	{
		return films.get(index).getDriveLocation();
	}
	public boolean isWatched(int index)
	{
		return films.get(index).isWatched();
	}
	public void setWatched(boolean w, int index)
	{
		System.out.println("Watched was: " + films.get(index).isWatched());
		films.get(index).setWatched(w);
		System.out.println("Watched is now: " + films.get(index).isWatched());
	}
	public void addMovie(Movie m, double st)
	{
		films.add(m);
		numFilms++;
		storageTotal += st;
	}
	
	public void buildCollection()
	{
		//filePath = {"E:\\", "D:\\Have Not Watched", "D:\\Have Watched", "H:\\ Have Watched", "H:\\ Have Not Watched"};
		if(!filePathSet)
		{
			filePath = new String[] {"C:\\Music"};
		}
		films = new ArrayList<Movie>();
		numFilms = 0;
		for(int i = 0; i < filePath.length; i++)
		{
			recursiveScanner(filePath[i]);
		}
		MainFrame.statusLabel.setText("Scan Completed!");
	}
	
	private void recursiveScanner(String filePath)
	{
		File folder = new File(filePath);
		File[] listOfFiles = folder.listFiles();
		
		String file = "", path = "";
		double s = 0;
		MainFrame.statusLabel.setText("Scanning: " + filePath);
		if(listOfFiles != null && listOfFiles.length > 0)
		{
			for(int i = 0; i < listOfFiles.length; i++)
			{
				if(listOfFiles[i].isFile())
				{
					file = listOfFiles[i].getName();
					path = listOfFiles[i].getAbsolutePath();
					s = listOfFiles[i].length();
					//if(file.endsWith(".mkv") || file.endsWith(".avi") || file.endsWith(".mp4"))
					if(file.endsWith(".mp3") || file.endsWith(".avi") || file.endsWith(".mp4"))
					{
						Movie m = new Movie(path, file);
						addMovie(m, s);
						
					}
				}
				else if(listOfFiles[i].isDirectory())
				{
					recursiveScanner(listOfFiles[i].getAbsolutePath());
				}
				else
				{
					MainFrame.statusLabel.setText("SOMETHING FUCKED UP!1one Blame the NSA.");
				}
			}
		}

	}

	public String getExportPath() {
		return exportPath;
	}

	public void setExportPath(String exportPath) {
		System.out.println("Set the export path");
		this.exportPath = exportPath;
	}
	
}
